package ext.acae2.dms.parse.wc9;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import wt.util.WTProperties;
import ext.acae2.dms.bean.DocReferenceLinkBean;
import ext.acae2.dms.bean.DocumentBean;
import ext.acae2.dms.bean.EPMBuildHistoryBean;
import ext.acae2.dms.bean.EPMDescribeLinkBean;
import ext.acae2.dms.bean.EPMDocumentBean;
import ext.acae2.dms.bean.EPMMemberLinkBean;
import ext.acae2.dms.bean.EPMReferenceLinkBean;
import ext.acae2.dms.bean.PartBean;
import ext.acae2.dms.bean.PartUsageLinkBean;
import ext.acae2.util.ParameterUtil;

public class ParseImportList {
	
	public static void main(String[] args) throws Exception {
		readtxt("d:\\L0000005512\\123.xml");
	}

	@SuppressWarnings("rawtypes")
	public static Map<String, List> readtxt(String xmlPath) throws Exception {
		
		String wthome = (String) WTProperties.getLocalProperties().get("wt.home");
//		String wthome = "D:/workspace/bch/ACAE_EPICCA_II/";
		String attrPathFolder = wthome + File.separator+"codebase"+File.separator+"ext"+File.separator+"acae2"+File.separator+"dms"+File.separator+"input" +File.separator+"614.ini";
		
		String rootFolderPath = xmlPath.substring(0,
				xmlPath.lastIndexOf(File.separator) + 1);
		
		File file = new File(rootFolderPath + "standardX10.dtd");
		System.out.println(rootFolderPath + "standardX10.dtd");
		file.createNewFile();
		
		List<PartBean> partBeanList = new ArrayList<PartBean>();
		List<EPMDocumentBean> epmDocumentBeanList = new ArrayList<EPMDocumentBean>();
		List<PartUsageLinkBean> partUsageLinkBeanList = new ArrayList<PartUsageLinkBean>();
		List<EPMMemberLinkBean> epmMemberLinkBeanList = new ArrayList<EPMMemberLinkBean>();
		List<EPMReferenceLinkBean> epmReferenceLinkBeanList = new ArrayList<EPMReferenceLinkBean>();
		List<EPMBuildHistoryBean> epmBuildHistoryBeanList = new ArrayList<EPMBuildHistoryBean>();
		
		BufferedReader br = new BufferedReader(new FileReader(rootFolderPath + "importList.txt"));
		String fileName = br.readLine();
		while (fileName != null) {
			System.out.println("fileName = " + fileName);
			String xmlType = fileName.substring(4,fileName.lastIndexOf("-"));
			System.out.println("xmlType = " + xmlType);
			String filePath = rootFolderPath + fileName;
			System.out.println("filePath = " + filePath);
			
			if(xmlType.equals("WTPart")){
				
				PartBean partBean = ParseWTPart.execute(filePath, attrPathFolder);
				partBeanList.add(partBean);
				
			}else if(xmlType.equals("EPMDocument")){
				
				EPMDocumentBean epmDocumentBean = ParseEPMDocument.execute(filePath, attrPathFolder);
				epmDocumentBeanList.add(epmDocumentBean);
				
			}else if(xmlType.equals("WTPartUsageLink")){
			
				PartUsageLinkBean partUsageLinkBean = ParseWTPartUsageLink.execute(filePath);
				partUsageLinkBeanList.add(partUsageLinkBean);
				
			}else if(xmlType.equals("EPMMemberLink")){	
			
				EPMMemberLinkBean epmMemberLinkBean = ParseEPMMemberLink.execute(filePath);
				epmMemberLinkBeanList.add(epmMemberLinkBean);
				
			}else if(xmlType.equals("EPMReferenceLink")){
				
				EPMReferenceLinkBean epmReferenceLinkBean = ParseEPMReferenceLink.execute(filePath);
				epmReferenceLinkBeanList.add(epmReferenceLinkBean);
				
			}else if(xmlType.equals("EPMBuildHistory")){
				
				EPMBuildHistoryBean epmBuildHistoryBean = ParseEPMBuildHistory.execute(filePath);
				epmBuildHistoryBeanList.add(epmBuildHistoryBean);
				
			}else if(xmlType.equals("AdHocAclEntrySet")){
				
			}else if(xmlType.equals("BaselineMember")){
				
			}else if(xmlType.equals("ibaDefinitions")){
				
			}else if(xmlType.equals("Representation")){
				
			}else if(xmlType.equals("WorkPackage")){
				
			}else if(xmlType.equals("WPBaseline")){
				
			}else if(xmlType.equals("WTTypeDefinitions")){
				
			}
			
			fileName = br.readLine();
		}
		
		Map<String, List> map = new HashMap<String, List>();
		map.put(ParameterUtil.PARTBEAN, partBeanList);
		map.put(ParameterUtil.EPMDOCUMENTBEAN, epmDocumentBeanList);
		map.put(ParameterUtil.PARTUSAGELINKBEAN, partUsageLinkBeanList);
		map.put(ParameterUtil.EPMMEMBERLINK, epmMemberLinkBeanList);
		map.put(ParameterUtil.EPMREFERENCELINK, epmReferenceLinkBeanList);
		map.put(ParameterUtil.EPMBUILDHISTORYLINK, epmBuildHistoryBeanList);
		//printBean(map);
		return map;
	}
	
	// Temporary test (remove in the future)
		@SuppressWarnings({ "rawtypes", "unchecked" })
		private static void printBean(Map<String, List> map) {

			System.out.println("<<< Start printBean");

			List<PartBean> partList = map.get(ParameterUtil.PARTBEAN);
			if(partList != null){
				for (int i = 0; i < partList.size(); i++) {
					System.out.println("！！！ PartBean " + i + " Start ！！！");
					System.out.println(" partId = " + partList.get(i).getId());
					System.out.println(" partName = " + partList.get(i).getName());
					System.out.println(" partNo = " + partList.get(i).getNumber());
					System.out.println(" partParentId = " + partList.get(i).getParentId());
					System.out.println(" partState = " + partList.get(i).getState());
					System.out.println(" partType = " + partList.get(i).getType());
					System.out.println(" partVersion = " + partList.get(i).getVersion());

					// attributes
					System.out.println(" ");
					System.out.println("	！！！ ATTRIBUTES Start ！！！");
					Set<String> keys = partList.get(i).getMap().keySet();
					for(String key:keys){
						System.out.println("### "+ key + " = " + partList.get(i).getMap().get(key) + " ###");
					}
					System.out.println("	！！！ ATTRIBUTES End ！！！");
					System.out.println(" ");

					System.out.println("！！！ PartBean " + i + " End ！！！");
					System.out.println(" ");
				}
			}
			
			List<EPMDocumentBean> epmDocList = map
					.get(ParameterUtil.EPMDOCUMENTBEAN);
			if(epmDocList != null){
				for (int i = 0; i < epmDocList.size(); i++) {
					System.out.println("！！！ EPMDocumentBean " + i + " Start ！！！");
					System.out.println(" epmDocId = " + epmDocList.get(i).getId());
					System.out.println(" epmDocName = " + epmDocList.get(i).getName());
					System.out.println(" epmDocNo = " + epmDocList.get(i).getNumber());
					System.out.println(" epmDocPartId = " + epmDocList.get(i).getPartId());
					System.out.println(" epmDocState = " + epmDocList.get(i).getState());
					System.out.println(" epmDocType = " + epmDocList.get(i).getType());
					System.out.println(" epmDocVersion = " + epmDocList.get(i).getVersion());
					System.out.println(" epmDocLocationRef = "
							+ epmDocList.get(i).getLocationRef());

					// attributes
					System.out.println(" ");
					System.out.println("	！！！ ATTRIBUTES Start ！！！");
					Set<String> keys = epmDocList.get(i).getMap().keySet();
					for(String key:keys){
						System.out.println("### "+ key + " = " + epmDocList.get(i).getMap().get(key) + " ###");
					}
					System.out.println("	！！！ ATTRIBUTES End ！！！");
					System.out.println(" ");

					System.out.println("！！！ EPMDocumentBean " + i + " End ！！！");
					System.out.println(" ");
				}
			}
			
			List<PartUsageLinkBean> partLinkList = map
					.get(ParameterUtil.PARTUSAGELINKBEAN);
			if(partLinkList != null){
				for (int i = 0; i < partLinkList.size(); i++) {
					System.out.println("！！！ PartUsageLinkBean " + i + " Start ！！！");
					System.out.println(" parentPartId = "
							+ partLinkList.get(i).getParentPart().getId());
					System.out.println(" childPartId = "
							+ partLinkList.get(i).getChildPart().getId());
					System.out.println("！！！ PartUsageLinkBean " + i + " End ！！！");
					System.out.println(" ");
				}
			}

			List<EPMDescribeLinkBean> epmDocLinkList = map
					.get(ParameterUtil.EPMDESCRIBELINKBEAN);
			if(epmDocLinkList != null){
				for (int i = 0; i < epmDocLinkList.size(); i++) {
					System.out.println("！！！ EPMDescribeLinkBean " + i + " Start ！！！");
					System.out.println(" partId = " + epmDocLinkList.get(i).getPart().getId());
					System.out.println(" epmDocId = "
							+ epmDocLinkList.get(i).getEpmDocument().getId());
					System.out.println("！！！ EPMDescribeLinkBean " + i + " End ！！！");
					System.out.println(" ");
				}
			}
			
			List<DocReferenceLinkBean> docLinkList = map
					.get(ParameterUtil.DOCREFERENCELINKBEAN);
			if(docLinkList != null){
				for (int i = 0; i < docLinkList.size(); i++) {
					System.out.println("！！！ DocReferenceLinkBean " + i + " Start ！！！");
					System.out.println(" partId = " + docLinkList.get(i).getPart().getId());
					System.out.println(" docId = " + docLinkList.get(i).getDocument().getId());
					System.out.println("！！！ DocReferenceLinkBean " + i + " End ！！！");
					System.out.println(" ");
				}
			}
			
			List<EPMMemberLinkBean> epmMemberLinkList = map
					.get(ParameterUtil.EPMMEMBERLINK);
			if(epmMemberLinkList != null){
				for (int i = 0; i < epmMemberLinkList.size(); i++) {
					System.out.println("！！！ EPMMemberLinkBean " + i + " Start ！！！");
					System.out.println(" ParentEpm = " + epmMemberLinkList.get(i).getParentEpm().getLocationRef());
					System.out.println(" ChildEpm = " + epmMemberLinkList.get(i).getChildEpm().getLocationRef());
					System.out.println("！！！ EPMMemberLinkBean " + i + " End ！！！");
					System.out.println(" ");
				}
			}
			
			List<EPMReferenceLinkBean> epmReferenceLinkList = map
					.get(ParameterUtil.EPMREFERENCELINK);
			if(epmReferenceLinkList != null){
				for (int i = 0; i < epmReferenceLinkList.size(); i++) {
					System.out.println("！！！ EPMReferenceLinkBean " + i + " Start ！！！");
					System.out.println(" partId = " + epmReferenceLinkList.get(i).getRefEpmBean().getLocationRef());
					System.out.println(" docId = " + epmReferenceLinkList.get(i).getEpmBean().getLocationRef());
					System.out.println("！！！ EPMReferenceLinkBean " + i + " End ！！！");
					System.out.println(" ");
				}
			}
			
			System.out.println("<<< End printBean");
		}

}
